package com.domain.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.domain.oa.entity.Role;
import com.domain.oa.mapper.MenuMapper;
import com.domain.oa.mapper.RoleMapper;
import com.domain.oa.service.RoleService;
import com.domain.oa.utils.ResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Role)表服务实现类
 *
 * @author makejava
 * @since 2023-02-20 15:50:46
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Resource
    private MenuMapper menuMapper;

    @Resource
    private RoleMapper roleMapper;

    @Override
    public ResultVo getDataByPage(Integer page, Integer pageSize) {
        Page<Role> page1 = new Page<>(page, pageSize);
        QueryWrapper<Role> query = new QueryWrapper<>();
        Page<Role> result = page(page1, query);
        return new ResultVo(200, "分页查询权限成功！", result);
    }

    @Override
    public ResultVo editData(Role data) {
        try {
            saveOrUpdate(data);
            return new ResultVo(200, "修改权限成功！",null);
        } catch (Exception e) {
            return new ResultVo(400, "操作权限失败！", e.getMessage());
        }

    }

    @Override
    public ResultVo deleteDataById(Integer id) {
        try {
            removeById(id);
            return new ResultVo(200, "删除权限成功！",null);
        } catch (Exception e) {
            return new ResultVo(400, "删除权限失败！", e.getMessage());
        }
    }

    @Override
    public ResultVo getRoleMenuByRoleId(Integer roleId) {
        try {
            List<Integer> result = menuMapper.getRoleMenuByRoleId(roleId);
            return new ResultVo(200, "获取角色相关联的权限成功!", result);
        }catch (Exception e) {
            return new ResultVo(505, "获取角色相关联的权限失败!", e.getMessage());
        }

    }

    @Override
    public ResultVo addRoleMenu(Integer roleId, Integer menuId) {
        try {
            List<Integer> roleMenuByRoleId = menuMapper.getRoleMenuByRoleId(roleId);
            for (Integer i : roleMenuByRoleId) {
                if(i == menuId) {
                    return new ResultVo(201, "系统已存在该权限!", null);
                }
            }
            Integer integer = roleMapper.addRoleByMenuId(roleId, menuId);
            if(integer == 1) {
                return new ResultVo(200, "添加权限成功!", null);
            }else {
                return new ResultVo(505, "添加权限失败!", null);
            }
        }catch (Exception e) {
            return new ResultVo(505, "添加权限失败!", e);
        }
    }

    @Override
    public ResultVo removeRoleMenu(Integer roleId, Integer menuId) {
        try {
            Integer integer = roleMapper.removeRoleByMenuId(roleId, menuId);
            if(integer == 1) {
                return new ResultVo(200, "删除权限成功!", null);
            }else {
                return new ResultVo(201, "添加权限失败!", null);
            }
        }catch (Exception e) {
            return new ResultVo(505, "系统错误！", e.getMessage());
        }
    }

    @Override
    public ResultVo getAllRole() {
        try {
            List<Role> allRole = roleMapper.getAllRole();
            return new ResultVo(200, "获取所有权限成功！", allRole);
        }catch (Exception e) {
            return new ResultVo(201, "获取所有权限失败！", null);
        }
    }
}

